package presentacion;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.ListSelectionModel;

import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class Datos_Productos extends JFrame {

	private JPanel contentPane;
	private final JToolBar toolBar = new JToolBar();
	private final JButton btnAniadirFila = new JButton("Añadir");
	private final JButton btnEliminarFila = new JButton("Eliminar");
	private final JScrollPane scrollPane = new JScrollPane();
	private final JButton btnVolverAlMen = new JButton("Volver al menú principal");
	private final JButton btnGuardarDatos = new JButton("Guardar Datos");
	private final JTable table = new JTable();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Datos_Productos frame = new Datos_Productos();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	public Datos_Productos() {
		setBounds(100, 100, 671, 434);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		MiModeloTabla modeloTabla = new MiModeloTabla();
		{
			btnAniadirFila.addActionListener(new BtnAniadirFilaActionListener_1());
			toolBar.setSize(new Dimension(160, 20));
			toolBar.setAlignmentY(Component.CENTER_ALIGNMENT);
			toolBar.add(btnAniadirFila);
		}
		{
			btnEliminarFila.addActionListener(new BtnEliminarFilaActionListener());
			toolBar.add(btnEliminarFila);
		}
		{
			
			Object[] fila1= {  "1234", "camel", "20", "2x1x0.5", "799.99", "mueble cómoda"};
			modeloTabla.aniadeFila(fila1);
			Object[] fila2= {  "2142", "azul", "22", "2x0.5x2", "599.99", "cama matrimonio"};
			modeloTabla.aniadeFila(fila2);
			Object[] fila3= {  "1121", "nogal", "20", "5x2.5x0.5", "699.99", "armario empotrado 4 puertas"};
			modeloTabla.aniadeFila(fila3);
			contentPane.setLayout(new BorderLayout(0, 0));
			contentPane.add(toolBar, BorderLayout.NORTH);
			btnVolverAlMen.addActionListener(new BtnVolverAlMenActionListener());
			
			toolBar.add(btnVolverAlMen);
			//btnGuardarDatos.addChangeListener(new BtnGuardarDatosChangeListener());
			
			toolBar.add(btnGuardarDatos);
			contentPane.add(scrollPane);
			
		}
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{new Integer (1234), "camel", new Integer(20), "2x1x0.5", 799.99, "mueble cómoda"},
				{new Integer (2142), "azul", new Integer (22), "2x0.5x2", 599.99, "cama matrimonio"},
				{new Integer (1121), "nogal", new Integer (20), "5x2.5x0.5", 699.99, "armario empotrado 4 puertas"},
			},
			new String[] {
				"ID", "Color", "Cantidad", "Medidas (ancho x alto x profundo)", "Precio", "Descripcion"
			}
		) {
			Class[] columnTypes = new Class[] {
				Integer.class, String.class, Integer.class, String.class, Double.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		/*Object[] fila1= {  new Integer (1234), "camel", new Integer(20), "2x1x0.5", 799.99, "mueble cómoda"};
		modeloTabla.aniadeFila(fila1);
		Object[] fila2= { new Integer (2142), "azul", new Integer (22), "2x0.5x2", 599.99, "cama matrimonio"};
		modeloTabla.aniadeFila(fila2);
		Object[] fila3= { new Integer (1121), "nogal", new Integer (20), "5x2.5x0.5", 699.99, "armario empotrado 4 puertas"};
		modeloTabla.aniadeFila(fila3);*/
		
		scrollPane.setViewportView(table);
	}
	private class BtnAniadirFilaActionListener_1 implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			MiModeloTabla modeloTabla =  (MiModeloTabla) table.getModel();
			Object[] nuevaFila = {" ", " "," ", " "," ", " "};
			modeloTabla.aniadeFila(nuevaFila);
			modeloTabla.fireTableDataChanged();
		}
	}
	private class BtnEliminarFilaActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			MiModeloTabla modeloTabla = (MiModeloTabla) table.getModel();
			int n= table.getSelectedRow();
			if (n != -1) 
				modeloTabla.eliminaFila(table.getSelectedRow());
			modeloTabla.fireTableDataChanged();
		}
	}
	private class BtnVolverAlMenActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			dispose();
		}
	}
	/*private class BtnGuardarDatosChangeListener implements ChangeListener {
		public void stateChanged(ChangeEvent arg0) {
			table.
		}
	}*/
}
